<!DOC html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
</head>
<body>

<!--表格容器-->
<table id="demo" lay-filter="test"></table>



<!--头部工具栏：-->
<script type="text/html" id="toolbarDemo1">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>


<!--行内工具栏：-->
<script type="text/html" id="toolbarDemo2">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">编辑</button>
    </div>
</script>



<!--添加的表单弹窗-->
<form class="layui-form" action="" id="addForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">菜品ID</label>
        <div class="layui-input-block">
            <input type="text" name="proId" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数量</label>
        <div class="layui-input-block">
            <input type="text" name="num" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">桌ID</label>
        <div class="layui-input-block">
            <input type="text" name="deskId" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemoAdd">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<!--修改的表单弹窗-->
<form class="layui-form" action="" id="updateForm" lay-filter="updateForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">id</label>
        <div class="layui-input-block">
            <input type="text" name="id" required  lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜品ID</label>
        <div class="layui-input-block">
            <input type="text" name="proId" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数量</label>
        <div class="layui-input-block">
            <input type="text" name="num" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">桌ID</label>
        <div class="layui-input-block">
            <input type="text" name="deskId" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemoUpdate">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>







<!--脚本开始-->
<script src="js/layui/layui.js"></script>
<script>
    layui.use(['table','form'], function(){
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;



        //添加表单的监听提交
        form.on('submit(formDemoAdd)', function(data){
            // layer.msg(JSON.stringify(data.field));
            $.post("cart/addCart",data.field,function(res){
                if(res.code==200){
                    layer.msg("添加成功", {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        table.reload("idTest",{})
                        layer.closeAll();
                    });
                }else{
                    layer.msg("添加失败")
                }
            },"json")
            return false;
        });


        //修改表单的监听提交
        form.on('submit(formDemoUpdate)', function(data){
            // layer.msg(JSON.stringify(data.field));
            $.post("cart/updateCart",data.field,function(res){
                if(res.code==200){
                    layer.msg("修改成功", {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        table.reload("idTest",{})
                        layer.closeAll();
                    });
                }else{
                    layer.msg("修改失败")
                }
            },"json")
            return false;
        });



        //触发事件----头部工具条监听
        table.on('toolbar(test)', function(obj){
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    layer.open({
                        type: 1,
                        content: $('#addForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;

            };
        });



        //触发事件----行内工具条监听
        table.on('tool(test)', function(obj){
            switch(obj.event){
                case 'delete':
                    //layer.msg('删除');
                    layer.confirm('真的删除行么', function(index){
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        layer.close(index);
                        //向服务端发送删除指令
                        $.post("cart/deleteCart",obj.data,function(res){
                            if(res.code==200){
                                layer.msg("删除成功", {
                                    icon: 1,
                                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                                }, function(){
                                    table.reload("idTest",{})
                                    layer.closeAll();
                                });
                            }else{
                                layer.msg("删除失败")
                            }
                        },"json")
                    });
                    break;
                case 'update':
                    //layer.msg('编辑');
                    form.val("updateForm",obj.data) //给表单赋值
                    layer.open({
                        type: 1,
                        content: $('#updateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });



        //第一个实例
        table.render({
            elem: '#demo'
            ,id:'idTest'
            ,toolbar: '#toolbarDemo1'
            ,url: 'cart/getCartList' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', width:80}
                ,{field: 'proId', title: '菜品ID', width:180}
                ,{field: 'num', title: '数量', width:180}
                ,{field: 'deskId', title: '桌ID', width:180}
                ,{title: '操作', width:280,toolbar: '#toolbarDemo2'}
            ]]
        });

    });
</script>
</body>
</html>